package org.seedltd.upms.service.impl.detection;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.seedltd.base.exception.BusinessException;
import org.seedltd.core.base.BaseServiceImpl;
import org.seedltd.core.utils.StringUtils;
import org.seedltd.upms.enums.EnableStatus;
import org.seedltd.upms.mapper.detection.DetectionTerminalPipelineMapper;
import org.seedltd.upms.model.detection.entity.DetectionTerminalPipeline;
import org.seedltd.upms.model.detection.form.DetectionTerminalPipelineForm;
import org.seedltd.upms.model.detection.vo.CommodityInfo;
import org.seedltd.upms.model.detection.vo.DetectionTerminalPipelineVO;
import org.seedltd.upms.service.detection.DetectionTerminalPipelineService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

import lombok.AllArgsConstructor;

/**
 * 售卖机货道维护业务实现
 * 
 * @author laizuan
 * @since 2021/09/23
 */
@Service
@AllArgsConstructor
public class DetectionTerminalPipelineServiceImpl
    extends BaseServiceImpl<DetectionTerminalPipelineMapper, DetectionTerminalPipeline>
    implements DetectionTerminalPipelineService {

    @Override
    public DetectionTerminalPipeline getByTerminalId(Long terminalId) {
        List<DetectionTerminalPipeline> detectionTerminalPipelines =
            q().eq(DetectionTerminalPipeline::getTerminalId, terminalId)
                .eq(DetectionTerminalPipeline::getCommodityCondition, EnableStatus.ENABLE.getValue())
                .ge(DetectionTerminalPipeline::getPresentNumber, 1)
                .orderByDesc(DetectionTerminalPipeline::getPresentNumber).last("limit 1").selectList();

        if (CollectionUtils.isEmpty(detectionTerminalPipelines)) {
            throw new BusinessException("当前售卖机无可用货道，请联系客服");
        }
        return detectionTerminalPipelines.get(0);
    }

    @Override
    public boolean updatePipelineNumber(Long id, Integer presentNumber) {
        return u().eq(DetectionTerminalPipeline::getId, id)
            .set(DetectionTerminalPipeline::getPresentNumber, presentNumber).update();
    }

    @Override
    public List<DetectionTerminalPipelineVO> listByTerminalId(Long terminalId) {
        return q().eq(DetectionTerminalPipeline::getTerminalId, terminalId)
            .orderByDesc(DetectionTerminalPipeline::getCommodityCondition)
            .selectList(DetectionTerminalPipelineVO.class);
    }

    @Override
    @Transactional
    public boolean update(List<DetectionTerminalPipelineForm> forms) {
        Long terminalId = forms.get(0).getTerminalId();
        this.deleteByTerminalId(terminalId);
        DetectionTerminalPipeline detectionTerminalPipeline;
        List<DetectionTerminalPipeline> list = Lists.newArrayListWithCapacity(forms.size());
        for (DetectionTerminalPipelineForm form : forms) {
            detectionTerminalPipeline = new DetectionTerminalPipeline();
            detectionTerminalPipeline.setTerminalId(terminalId);
            detectionTerminalPipeline.setEquipmentUuid(form.getEquipmentUuid());
            detectionTerminalPipeline.setPresentNumber(form.getPresentNumber());
            detectionTerminalPipeline.setArrivalName(form.getArrivalName());
            detectionTerminalPipeline.setCommodityCondition(1);
            list.add(detectionTerminalPipeline);
        }
        return this.saveBatch(list);
    }

    @Override
    @Transactional
    public boolean update(List<CommodityInfo> commoditys, Long terminalId) {
        if (CollectionUtils.isEmpty(commoditys)) {
            return false;
        }
        List<DetectionTerminalPipeline> detectionTerminalPipelines =
            q().eq(DetectionTerminalPipeline::getTerminalId, terminalId).selectList();

        if (CollectionUtils.isEmpty(detectionTerminalPipelines)) {
            return false;
        }

        for (DetectionTerminalPipeline detectionTerminalPipeline : detectionTerminalPipelines) {
            String arrivalName = detectionTerminalPipeline.getArrivalName();
            String equipmentUuid = detectionTerminalPipeline.getEquipmentUuid();
            CommodityInfo commodityInfo =
                commoditys.stream().filter(f -> StringUtils.equals(f.getArrivalName(), arrivalName)
                    && StringUtils.equals(f.getEquipmentUuid(), equipmentUuid)).findFirst().orElse(null);
            if (commodityInfo != null) {
                detectionTerminalPipeline.setPresentNumber(commodityInfo.getPresentNumber());
                // detectionTerminalPipeline.setEquipmentUuid(commodityInfo.getEquipmentUuid());
                detectionTerminalPipeline.setCommodityCondition(commodityInfo.getCommodityCondition());
            }
        }

        return this.updateBatchById(detectionTerminalPipelines);
    }

    @Override
    public boolean deleteByTerminalId(Long terminalId) {
        return u().eq(DetectionTerminalPipeline::getTerminalId, terminalId).delete();
    }
}